package kingwing.dc.core.view.frame.main;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import kingwing.dc.core.model.MyUrls;
import kingwing.dc.core.orm.hibernate.CommonHibernateDao;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;

@Component
public class CrawlerMainFramePR  {

	@Resource( name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao commonHibernateDao;
	
	/**
	 * 依据parentId获取URL
	 * @param parentId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@DataProvider
	public Collection<MyUrls> getMyUrls(String parentId){
		IUser user = ContextHolder.getLoginUser();
		if (user == null) {
			throw new NoneLoginException("登陆超时，请登录！");
		}
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(MyUrls.class);
		if (StringUtils.isBlank(parentId)) {
			detachedCriteria.add(Restrictions.isNull("parentId"));
			return (Collection<MyUrls>) this.commonHibernateDao.query(detachedCriteria);
		}else {
			MyUrls myUrl = this.commonHibernateDao.get(parentId, MyUrls.class);
			parentId = myUrl.getId();
			String username = user.getUsername();
			detachedCriteria.add(Restrictions.eq("parentId", parentId));
			detachedCriteria.add(Restrictions.eq("createUserId",username));
			return (Collection<MyUrls>) this.commonHibernateDao.query(detachedCriteria);
		}
		
	}
	
	/**
	 * 根据parentId获取子节点URL
	 * @param parentId
	 * @return
	 */
	@DataProvider
	public Collection<MyUrls> loadContainChildMeunUrls(String parentId) {
		Collection<MyUrls> result = this.getMyUrls(parentId);
		this.loadContainChildMeunUrls(result, parentId);
		return result;
	}

	private void loadContainChildMeunUrls(Collection<MyUrls> result, String parentId) {
		for (MyUrls myUrl : result) {
			List<MyUrls> childList = new ArrayList<MyUrls>();
			childList.addAll(this.getMyUrls(myUrl.getId()));
			myUrl.setChildren(childList);
			this.loadContainChildMeunUrls(childList, myUrl.getId());
		}
	}
	
	/**
	 * 根据name删除菜单
	 * @param name
	 * @return
	 */
	@Transactional
	@Expose
	public boolean deleteMysql(final String name,final String username){
		
		/*final String username = ContextHolder.getLoginUserName();
		if (StringUtils.isBlank(username)) {
			throw new NoneLoginException("登陆超时，请登录！");
		}*/
		
		if (StringUtils.isNotBlank(name)) {
			return (boolean) this.commonHibernateDao.doInHibernateSession(new ISessionCallback<Object>() {
				
				@Override
				public Object doInSession(Session session) {
					// TODO Auto-generated method stub
					session.createQuery("delete MyUrls where name=:name and createUserId=:username")
					.setString("name", name)
					.setString("username", username)
					.executeUpdate();
					return true;
				}
			});
		}
		
		return false;
	}
	
}
